In [1]:
import pandas as pd
In [2]:
df_products = pd.read_csv('products.csv', sep=';')
df_products
Out[2]:
id title brand_id
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1
1 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2
2 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3
3 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3
4 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1
5 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4
6 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5
7 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2
8 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3
9 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6
In [3]:
df_brands = pd.read_csv('brands.csv', sep=';')
df_brands
Out[3]:
brand_id brand
0 1 Acer
1 2 Asus
2 3 HP
3 4 Dell
4 5 Apple
5 6 Lenovo
In [5]:
pd.merge(df_products, df_brands, on='brand_id')
Out[5]:
id title brand_id brand
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1 Acer
1 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1 Acer
2 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2 Asus
3 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2 Asus
4 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3 HP
5 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3 HP
6 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3 HP
7 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3 HP
8 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4 Dell
9 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5 Apple
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6 Lenovo
In [6]:
df_brands.loc[6] = [7, 'MSI']
In [7]:
df_brands
Out[7]:
brand_id brand
0 1 Acer
1 2 Asus
2 3 HP
3 4 Dell
4 5 Apple
5 6 Lenovo
6 7 MSI
In [8]:
df_products.loc[11] = [12, 'Ноубук Huawei', 8]
In [9]:
df_products
Out[9]:
id title brand_id
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1
1 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2
2 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3
3 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3
4 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1
5 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4
6 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5
7 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2
8 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3
9 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6
11 12 Ноубук Huawei 8
In [10]:
pd.merge(df_products, df_brands, on='brand_id', how='inner')
Out[10]:
id title brand_id brand
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1 Acer
1 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1 Acer
2 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2 Asus
3 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2 Asus
4 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3 HP
5 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3 HP
6 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3 HP
7 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3 HP
8 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4 Dell
9 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5 Apple
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6 Lenovo
In [11]:
pd.merge(df_products, df_brands, on='brand_id', how='left')
Out[11]:
id title brand_id brand
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1 Acer
1 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2 Asus
2 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3 HP
3 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3 HP
4 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1 Acer
5 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4 Dell
6 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5 Apple
7 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2 Asus
8 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3 HP
9 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3 HP
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6 Lenovo
11 12 Ноубук Huawei 8 NaN
In [12]:
pd.merge(df_products, df_brands, on='brand_id', how='right')
Out[12]:
id title brand_id brand
0 1.0 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1 Acer
1 5.0 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1 Acer
2 2.0 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2 Asus
3 8.0 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2 Asus
4 3.0 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3 HP
5 4.0 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3 HP
6 9.0 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3 HP
7 10.0 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3 HP
8 6.0 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4 Dell
9 7.0 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5 Apple
10 11.0 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6 Lenovo
11 NaN NaN 7 MSI
In [13]:
pd.merge(df_products, df_brands, on='brand_id', how='outer')
Out[13]:
id title brand_id brand
0 1.0 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1 Acer
1 5.0 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1 Acer
2 2.0 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2 Asus
3 8.0 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2 Asus
4 3.0 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3 HP
5 4.0 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3 HP
6 9.0 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3 HP
7 10.0 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3 HP
8 6.0 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4 Dell
9 7.0 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5 Apple
10 11.0 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6 Lenovo
11 12.0 Ноубук Huawei 8 NaN
12 NaN NaN 7 MSI
In [14]:
df_brands
Out[14]:
brand_id brand
0 1 Acer
1 2 Asus
2 3 HP
3 4 Dell
4 5 Apple
5 6 Lenovo
6 7 MSI
In [15]:
df_brands.rename(columns={'brand_id': 'id'}, inplace=True)
In [16]:
df_brands
Out[16]:
id brand
0 1 Acer
1 2 Asus
2 3 HP
3 4 Dell
4 5 Apple
5 6 Lenovo
6 7 MSI
In [24]:
pd.merge(df_products, df_brands, left_on='brand_id', right_on='id', suffixes=('_left', '_right'), indicator=True, how='outer')
Out[24]:
id_left title brand_id id_right brand _merge
0 1.0 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1.0 1.0 Acer both
1 5.0 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1.0 1.0 Acer both
2 2.0 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2.0 2.0 Asus both
3 8.0 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2.0 2.0 Asus both
4 3.0 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3.0 3.0 HP both
5 4.0 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3.0 3.0 HP both
6 9.0 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3.0 3.0 HP both
7 10.0 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3.0 3.0 HP both
8 6.0 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4.0 4.0 Dell both
9 7.0 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5.0 5.0 Apple both
10 11.0 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6.0 6.0 Lenovo both
11 12.0 Ноубук Huawei 8.0 NaN NaN left_only
12 NaN NaN NaN 7.0 MSI right_only
In [ ]:
 
In [25]:
df_brands
Out[25]:
id brand
0 1 Acer
1 2 Asus
2 3 HP
3 4 Dell
4 5 Apple
5 6 Lenovo
6 7 MSI
In [26]:
df_brands.set_index('id', inplace=True)
In [27]:
df_brands
Out[27]:
brand
id
1 Acer
2 Asus
3 HP
4 Dell
5 Apple
6 Lenovo
7 MSI
In [28]:
df_products.join(df_brands, on='brand_id', how='inner')
Out[28]:
id title brand_id brand
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1 Acer
4 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1 Acer
1 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2 Asus
7 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2 Asus
2 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3 HP
3 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3 HP
8 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3 HP
9 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3 HP
5 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4 Dell
6 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5 Apple
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6 Lenovo
In [ ]:
 

Homework

Объедините таблицы городов и стран, добавив к таблице городов наименование страны. В итоговом датафрейме должны быть следующие столбцы:

  • ID (ID города)
  • City (Название города)
  • CountryCode (Код страны)
  • Country (Название страны)
  • Population (Кол-во населения города)
  • Итоговый датафрейм сохраните в файл Result.xlsx
In [ ]:
 
In [ ]:
 
In [ ]: